//
//  NetworkTool.m
//  AffectionBee
//
//  Created by 李通 on 16/8/18.
//  Copyright © 2016年 chivalrous. All rights reserved.
//

#import "NetworkTool.h"

@implementation NetworkTool


//MYMARK: - 重设网络基本设置
+ (instancetype)sharedManager {
    
    static NetworkTool *instance;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{

        //1.实例化
        NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
        instance = [[NetworkTool alloc] initWithSessionConfiguration:config];
        
        //2.可接受格式
        instance.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", nil];
        
        //3.请求格式（默认：二进制）
        instance.requestSerializer = [AFJSONRequestSerializer serializer];
        
        //4.设置超时时间
        config.timeoutIntervalForRequest = 15;
    });
    return instance;
}


//MYARK: - 根据网络请求类型封装不同网络请求方法
- (void)oauthLoadDataWithNetworkToolType:(NetworkToolType)type UrlString:(NSString *)urlString parameters:(NSDictionary *)parameters SuccessBlock:(SuccessCompletedBlock)success failure:(FailureCompletedBlock)failure {
    ABLog(@"1");
    //如果请求类型为GET请求
    if (type == NetworkToolTypeGET) {
        
        [[NetworkTool sharedManager] GET:urlString parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
            
            //如果网络请求结果不为空则回调block
            if (responseObject) {
                success(responseObject);
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            if (error) {
                failure(error);
            }
        }];
        //如果不是GET请求，那么就是POST请求
    } else {
        
        [[NetworkTool sharedManager] POST:urlString parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
            
            if (responseObject) {
                success(responseObject);
            }
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            if (error) {
                failure(error);
            }
        }];
    }
}
#pragma mark --超市数据--
-(void)superMarketLoadDataWithUrlStr:(NSString*)urlStr parameters:(NSDictionary *)parameters SuccessBlock:(SuccessCompletedBlock)success failure:(FailureCompletedBlock)failure {
    
    [self oauthLoadDataWithNetworkToolType:NetworkToolTypePOST UrlString:urlStr parameters:parameters SuccessBlock:^(id responseObject) {
        
        
        if (responseObject) {
            
            success(responseObject);
        }
        
    } failure:^(NSError *error) {
        
        if (error) {
            
            failure(error);
        }
        
    }];
    
}

/*
-(void)GetFirstCell
{
    //(1)urlString

    NSString *urlString = @"http://iosapi.itcast.cn/loveBeen/supermarket.json.php";
    
    //(2)参数
    NSDictionary *param = @{@"call":@5};

    NSString *urlString = @"http://iosapi.itcast.cn/loveBeen/promotion.json.php";
    
    //(2)参数
    NSDictionary *param = @{@"call":@8};

    
    //(3)POST请求
    [self oauthLoadDataWithNetworkToolType:NetworkToolTypePOST UrlString:urlString parameters:param SuccessBlock:^(NSDictionary *responseObject) {


        

        
        //NSLog(@"%@",responseObject);
        

        //NSLog(@"%@",responseObject);
        


        NSDictionary *dataDict = responseObject[@"data"];
        
        
        //商品
        NSLog(@"%@",dataDict[@"products"]);
        
        //分类
        NSLog(@"%@",dataDict[@"categories"]);
        
        
    } failure:^(NSError *error) {
        
        
        NSLog(@"获取信息失败");
        
    }];
    
}
*/

@end
